package com.incar.chuxing.utils.Dialogs;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.incar.chuxing.R;

/**
 * Created by user1 on 2017/6/29.
 */

public class LoadingDialog extends ProgressDialog {
    private static final int CHANGE_TITLE_WHAT = 1;
    private static final int CHNAGE_TITLE_DELAYMILLIS = 300;
    private static final int MAX_SUFFIX_NUMBER = 3;
    private static final char SUFFIX = '.';

    private String title;
    private TextView loading_title;
    private ImageView iv_rotate;

  /*  private Handler handler = new Handler() {
        private int num = 0;


        public void handleMessage(android.os.Message msg) {
            if (msg.what == CHANGE_TITLE_WHAT) {
                StringBuilder builder = new StringBuilder();
                if (num >= MAX_SUFFIX_NUMBER) {
                    num = 0;
                }
                num++;
                for (int i = 0; i < num; i++) {
                    builder.append(SUFFIX);
                }
                loading_dots.setText(builder.toString());
                if (isShowing()) {
                    handler.sendEmptyMessageDelayed(CHANGE_TITLE_WHAT, CHNAGE_TITLE_DELAYMILLIS);
                } else {
                    num = 0;
                }
            }
        }
    };*/


    public LoadingDialog(Context context) {
        super(context, R.style.Loading_Dialog_Dim);
    }


    public LoadingDialog(Context context, boolean isTrans) {
        super(context, isTrans ? R.style.Loading_Dialog_Transparent : R.style.Loading_Dialog_Dim);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.loading_dialog_layout);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(params);
        loading_title = (TextView) findViewById(R.id.loading_title);
        loading_title.setText(title);
        iv_rotate = (ImageView) findViewById(R.id.iv_rotate);
        initAnim();
    }


    @Override
    public void show() {//在要用到的地方调用这个方法
        // handler.sendEmptyMessage(CHANGE_TITLE_WHAT);
        super.show();
    }


    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void onStart() {
        iv_rotate.startAnimation(mAnim);
        loading_title.setText(title);
        super.onStart();
    }

    @Override
    protected void onStop() {
        mAnim.cancel();
        super.onStop();
    }

    public void setTitle(CharSequence title) {
        if (TextUtils.isEmpty(title)) {
            this.title = " 正在加载...";
        } else {
            this.title = " " + title.toString();
        }
    }


    private RotateAnimation mAnim;

    private void initAnim() {
// mAnim = new RotateAnimation(360, 0, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
        mAnim = new RotateAnimation(0, 360, Animation.RESTART, 0.5f, Animation.RESTART, 0.5f);
        mAnim.setDuration(1000);
        mAnim.setRepeatCount(Animation.INFINITE);
        mAnim.setRepeatMode(Animation.RESTART);
        mAnim.setStartTime(Animation.START_ON_FIRST_FRAME);
    }

}